// pages/detail/detail.js
import {
  network
} from "../index/network.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    var type = options.type;
    var id = options.id;    
    var that = this;
    that.setData({
      id:id,
      type:type
    })
    wx.showLoading({
      title: '正在加载中...',
    })

    
    
    if (type === 'tv') {
      var tvsUrl = 'https://m.douban.com/rexxar/api/v2/tv/'+id;
      network.getDetail({
        success: function (items) {
          // console.log(items);
          // console.log(items.rating.value);
          wx.hideLoading();
          

          var genres = items.genres;
          genres = genres.join("/");
          items.genres = genres;

          var actorNames = [];
          var actors = items.actors;
          if(actors.length > 3){
            actors =  actors.slice(0,3);
          }
          for (let index = 0; index < actors.length; index++) {
            let actor = actors[index]
            actorNames.push(actor.name);
            
          }
          actorNames = actorNames.join("/");
          var director = items.directors[0].name;
          var authors = director + "(导演) /" + actorNames ;
          items.authors = authors;

          that.setData({
            item: items
          });
        }
      }, tvsUrl);
    }else if (type === 'show') {
      var showUrl = 'https://m.douban.com/rexxar/api/v2/tv/'+id;
      network.getDetail({
        success: function (items) {
          console.log(items);
          // console.log(items.rating.value);
          wx.hideLoading();
          
          var genres = items.genres;
          genres = genres.join("/");
          items.genres = genres;

          var actorNames = [];
          var actors = items.actors;
          if(actors.length > 3){
            actors =  actors.slice(0,3);
          }
          if (actors.length==0) {
            actors.push({name:""});
            console.log(actors)
          }
          var actor = "";
          for (var index = 0; index < actors.length; index++) {
            actor = actors[index];            
            console.log(actor.name)    
            actorNames.push(actor.name);              
          }
       
          actorNames = actorNames.join("/");
          var director = items.directors[0].name?items.directors[0].name:"";
          var authors = director + "(导演) /" + actorNames ;
          items.authors = authors;

          that.setData({
            item: items
          });
        }
      }, showUrl);
    };

    network.getTags({
      type:type,
      id:id,
      success:function(tags){
        that.setData({
          tags:tags
        });
      }     
    });

    network.getComment({
      id:id,
      start:0,
      count:3,
      success:function(data){
        var commentTotal = data.total;
        var comments = data.interests;
        that.setData({
          commentTotal:commentTotal,
          comments:comments
        })
        // console.log(data)
        // console.log(comments)
        // console.log(commentTotal)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.pageScrollTo({
      scrollTop: 0,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})